package com.example.administrator.funchat.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.administrator.funchat.Contans.Config;
import com.example.administrator.funchat.R;
import com.example.administrator.funchat.activity.ChatActivity;
import com.example.administrator.funchat.adapter.ContactAdapter;
import com.example.administrator.funchat.model.UserData;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * 通讯录fragment加载
 * Created by Administrator on 2016/6/23.
 */
public class Contact_fragment extends android.support.v4.app.Fragment {

    private ListView listView;
    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.contact_tab, container, false);

        return view;
    }


    private ContactAdapter adapter;
    private UserData data;

    @Override
    public void onStart() {
        super.onStart();
        String url = Config.USER_DATA;
        RequestParams p = new RequestParams();
        p.addBodyParameter("phone", LoginFragment.userPhone);
        new HttpUtils().send(HttpRequest.HttpMethod.POST, url, p, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.i("user", "onSuccess: ==" + responseInfo.result);
                Gson gson = new Gson();
                data = gson.fromJson(responseInfo.result, UserData.class);//获取好友列表
                adapter = new ContactAdapter(getActivity(), data.getUserData());
                listView = (ListView) view.findViewById(R.id.mail_listview);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getContext(), ChatActivity.class);
                        intent.putExtra("friendInfo", data.getUserData().get(position));
                        startActivity(intent);
                    }
                });
                listView.setAdapter(adapter);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.i("user", "onFailure: ==" + e.getMessage() + "===" + s);
            }
        });

         registerReceive();//注册广播
    }

    private myBroadcastReceive receiver;

    public void registerReceive() {
        receiver = new myBroadcastReceive();
        IntentFilter filter = new IntentFilter(RECEIVE_NUM_MSG);
        getActivity().registerReceiver(receiver, filter);

    }

    public static final String RECEIVE_NUM_MSG = "com.example.administrator.num";

    //接受消息数目
    class myBroadcastReceive extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                int num = intent.getIntExtra("num", -1);
                Log.i("message", "onReceive: num=="+num);
                String send_phone = intent.getStringExtra("send_phone");
                if (send_phone != null && num != -1) {
                    for (int i = 0; i < data.getUserData().size(); i++) {
                        if (send_phone.equals(data.getUserData().get(i).getPhone())) {
                            data.getUserData().get(i).setNum(num);
                        }
                    }
                    adapter.notifyDataSetChanged();
                }


            }

        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }
}
